// pages/TradingAreaCenter/addShopCircle.js
const controller = require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    diaLogBoxShow: false,
    icon1: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210517/843796309306507264.png",
    icon2: "https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210517/843796492605980672.png",
    value: {
      provinceName: '',
      cityName: '',
      districtName: '',
      shopIds: '',
      name: "",
      image: "",
      districtId: "",
      context:""
    },
    shopList: [],
    selectShops:[],
    adminName:"",
    shopName:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAreaList()
    this.getShopList()
    if(options.id){
      this.getShopDetail(options.id)
    }
  },
  getShopDetail(id){
    controller.getShopCircleDetail({shopGroupId:id}).then(res=>{
      console.log(res.data);
      const value = {
        provinceName: res.data.provinceName,
        cityName: res.data.cityName,
        districtName: res.data.districtName,
        shopIds: res.data.shopIds,
        name: res.data.name,
        image: res.data.image,
        districtId: res.data.districtId,
        context:res.data.context
      }
      this.setData({
        selectShops:res.data.shopList,
        value
      })
      
    })
  },
  //确定上传店铺
  uploadShop(){
    let list=[]
    this.data.shopList.forEach(item=>{
      if(item.select==true){
        list.push(item)
      }
    })
    this.setData({
      selectShops:list,
      diaLogBoxShow:false
    })
  },
  //获取商圈简介
  getContext(e){
    this.setData({
      'value.context':e.detail.value
    })
  },

  //删除店铺
  delShop(e){
    let index=e.currentTarget.dataset.index
    let list=this.data.selectShops
    let shopList=this.data.shopList
    for(let i=0;i<shopList.length;i++){
      console.log(shopList[i])
      console.log(list[index])
      if(shopList[i].id==list[index].id){
        shopList[i].select=false
      }
    }
    list.splice(index,1)
    this.setData({
      selectShops:list,
      shopList:shopList
    })
  },

  //获取输入门店名称
  getInputShopName(e){
    this.setData({
      shopName:e.detail.value
    })
  },

  //获取输入负责人名称
  getInputAdminName(e){
    this.setData({
      adminName:e.detail.value
    })
  },


  //获取代理商下的商家列表
  getShopList() {
    let value = {}
    value.state = 0
    value.shopName=this.data.shopName
    value.adminName=this.data.adminName
    controller.myShop(value).then(res => {
      let list = res.data
      list.forEach(item => {
        item.select = false
      })
      this.setData({
        shopList: list
      });
    })
  },

  //选择店铺
  selectShop(e) {
    let index = e.currentTarget.dataset.index
    let list = this.data.shopList
    if (list[index].select == false) {
      list[index].select = true
    } else {
      list[index].select = false
    }
    this.setData({
      shopList: list
    })
  },

  //提交平台审核
  examineShopCircle() {
    let list = this.data.shopList
    let shopList = []
    list.forEach(item => {
      if (item.select == true) {
        shopList.push(item.id)
      }
    })
    if (shopList.length <= 0) {
      wx.showToast({
        icon: "none",
        title: '请选择店铺',
        duration: 1500
      })
      return
    }
    let value = this.data.value
    value.shopIds = shopList.join(",")
    controller.createShopGroup(value).then(res => {
      wx.showToast({
        icon: "none",
        title: '商圈提交审核成功',
        duration: 1500
      })
      setTimeout(function () {
        wx.navigateBack()
      }, 1200)
    })
  },

  //返回
  back() {
    wx.navigateBack({
      delta: 0,
    })
  },

  toSelectAdd() {
    this.setData({
      diaLogBoxShow: true
    })
  },
  fadeOut() {
    this.setData({
      diaLogBoxShow: false
    })
  },
  getName(e) {
    this.setData({
      'value.name': e.detail.value
    })
  },

  //获取省
  getAreaList() {
    controller.areaList({
      leveltype: 1
    }).then(res => {
      let shen = res.data;
      let id = 0;
      let shenList = "list[" + id + "]"
      this.setData({
        [shenList]: shen,
        shen: shen[0].name
      })
      this.getAreaListChildShi(res.data[0].id)
    })
  },

  //根据上层id获取市
  getAreaListChildShi(id) {
    controller.areaListChild({
      areaId: id
    }).then(res => {
      let shi = res.data;
      let id = 1;
      let shiList = "list[" + id + "]"
      this.setData({
        [shiList]: shi,
        shi: shi[0].name
      })
      this.getAreaListChildQu(res.data[0].id)
    })
  },

  //根据上层id获取区
  getAreaListChildQu(id) {
    controller.areaListChild({
      areaId: id
    }).then(res => {
      let qu = res.data;
      let id = 2;
      let quList = "list[" + id + "]"
      this.setData({
        [quList]: qu,
        qu: qu[0].name,
        quId: qu[0].id
      })
    })
  },

  change(e) {
    this.setData({
      'value.provinceName': this.data.shen,
      'value.cityName': this.data.shi,
      'value.districtName': this.data.qu,
      'value.districtId': this.data.quId
    })
    console.log(this.data.value);
  },
  columnChange(e) {
    let column = e.detail.column; //列
    let value = e.detail.value; //行

    let id = this.data.list[column][value].id;
    if (column == 0) {
      this.setData({
        shen: this.data.list[column][value].name
      })
      this.getAreaListChildShi(id);
    } else if (column == 1) {
      this.setData({
        shi: this.data.list[column][value].name
      })
      this.getAreaListChildQu(id);
    } else {
      this.setData({
        qu: this.data.list[column][value].name,
        quId: this.data.list[column][value].id
      })
    }
  },
  

 


  //上传图片
  uploadImg() {
    if (this.data.id) {
      return
    }
    let that = this;
    wx.chooseImage({
      count: 1,
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        const tempFiles = res.tempFiles;
        if (tempFiles[0].size > 1024 * 1024) {
          wx.showToast({
            title: '单张图片大不能超过1MB',
            icon: 'none',
            duration: 2000
          })
          return
        }
        wx.uploadFile({
          url: 'https://www.xlshhr.com/beauty-industry/api/img/uploadFile',
          filePath: tempFilePaths[0],
          name: 'image',
          formData: {
            'sessionId': wx.getStorageSync("sessionId")
          },
          success(res) {
            console.log(res);
            let src = JSON.parse(res.data)
            that.setData({
              'value.image': src.data
            })
          }
        })
      }
    })
  },
})